import java.util.PriorityQueue;

public class Solution2182 {
    public String repeatLimitedString(String s, int repeatLimit) {
        int[] f=new int[26];
        for (char c : s.toCharArray()) {
            f[c-'a']++;
        }
        PriorityQueue<int[]> priorityQueue=new PriorityQueue<>((a, b)->b[0]-a[0]);
        for (int i = 0; i < 26; i++) {
            if (f[i]!=0) {
                priorityQueue.offer(new int[]{i, f[i]});
            }
        }
        int[] temp=new int[2];
        temp[0]=-1;
        StringBuilder stringBuilder=new StringBuilder();
        while (!priorityQueue.isEmpty()){
            int[] t=priorityQueue.poll();
            if (temp[0]==-1){
                for (int i = 0; i <Math.min(t[1],repeatLimit);i++){
                    stringBuilder.append((char)(t[0]+'a'));
                }
                if (t[1]>repeatLimit) {
                    temp = new int[]{t[0], t[1] - repeatLimit};
                }
            }else{
                stringBuilder.append((char)(t[0]+'a'));
                if (t[1]>1){
                    priorityQueue.offer(new int[]{t[0],t[1]-1});
                }
                priorityQueue.offer(new int[]{temp[0],temp[1]});
                temp[0]=-1;
            }
        }
        return stringBuilder.toString();
    }

    public static void main(String[] args) {
        System.out.println(new Solution2182().repeatLimitedString("cczazcc",3));
    }
}
